package com.xyhuo.leetcode.january;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 反转字符串中的单词
 * https://leetcode.cn/problems/reverse-words-in-a-string/
 *
 * @time 2024/1/22
 */
public class ReverseWords {

    public String reverseWords(String s) {
        Deque<String> deque = new ArrayDeque<>();
        String[] split = s.split(" ");
        for (String str : split) {
            if (null == str || str.length() == 0) {
                continue;
            }
            deque.addLast(str);
        }
        StringBuilder sb = new StringBuilder();
        if (!deque.isEmpty()) {
            sb.append(deque.removeLast());
        }
        while (!deque.isEmpty()) {
            String first = deque.removeLast();
            sb.append(" ");
            sb.append(first);
        }
        return sb.toString();
    }
}
